package com.module.overview.ui.cardHistoryDetails.provider

import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.circle.CirclesMedalModel
import com.common.app.data.bean.home.ScoringFactorsModel
import com.common.app.data.bean.overView.HistoryDetailsModel
import com.common.app.data.repository.CircleApi
import com.common.app.data.repository.HomeApi
import com.common.app.helper.RouterHelper
import com.common.app.utls.TimeUtils
import com.common.base.utils.SpanUtils
import com.common.base.utils.ViewUtils.getColor
import com.common.base.utils.ViewUtils.getString
import com.module.ble.utils.AllCardTypeConfig
import com.module.overview.R
import com.common.app.widget.AnimatedRoundProgressBar
import com.common.base.app.extras.doOnUiThread
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.data.CommonBean
import com.common.base.data.ListCommon
import com.common.base.utils.ToastUtils
import com.common.base.widget.CustomTextView
import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import rxhttp.toFlow
import rxhttp.wrapper.param.RxHttp

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/7/11
 *
 * 描述：体征-综合状态卡片
 *
 * 修订历史：完成度100%
 *
 */
class OvSignsStateCardProvider(time:Long,
                               override val itemViewType: Int = AllCardTypeConfig.TYPE_CARD_SIGNS_STATE_VALUE,
                               override val layoutId: Int = R.layout.overview_signs_state_crad_provider
) : BaseItemProvider<HistoryDetailsModel>() {

    private var curTime = time

    private var requestJob: Job? = null
    var scope = CoroutineScope(Dispatchers.IO)

    var disposable: Disposable? = null

    private var tvSleepValue: TextView? = null

    init {
    }

    override fun convert(helper: BaseViewHolder, item: HistoryDetailsModel) {

        getSleepScoringFactors()
    }


    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        tvSleepValue = viewHolder.getView(R.id.tvSleepValue)
        viewHolder.getView<CustomTextView>(R.id.tvFactorsHint)
            .setOnClickListener(onClick = object : View.OnClickListener {
                override fun onClick(p0: View?) {
                    RouterHelper.navigationPostcard(RouterHub.ROUTER_HOME_SCORING_FACTORS)
                        .withInt(KeyBundle.BUNDLE_KEY, 3)
                        .withString(KeyBundle.EXPAND_DATA, TimeUtils.getYearMonthDay(curTime))
                        .navigation()
                }
            })

        tvSleepValue?.let {tvSleepValueIt->
            SpanUtils.create()
                .addForeColorSection(
                    "${getString(R.string.common_rail_unit)}\n",
                    getColor(R.color.c_f360a9)
                )
                .setAbsSize("${getString(R.string.common_rail_unit)}\n", 44)
                .setStyle("${getString(R.string.common_rail_unit)}\n", Typeface.BOLD)
                .addSection(getString(R.string.common_rail_unit))
                .showIn(tvSleepValueIt)
        }
        super.onViewHolderCreated(viewHolder, viewType)
    }

    // 手动取消请求
    fun cancelRequest() {
        requestJob?.cancel()
        requestJob = null
    }

    private fun getSleepScoringFactors() {
        // 取消之前的请求
        requestJob?.cancel()

        requestJob = scope.launch {
            RxHttp.postJson(HomeApi.GET_SIGNS_SCORING_FACTORS)
                .add("date", TimeUtils.getYearMonthDay(curTime))
                .toFlow<CommonBean<ScoringFactorsModel>>()
                .catch { e ->
                    // 处理异常
                }.collect { responseIt->
                    responseIt.isSuccess().yes {
                        responseIt.Data?.let { dataIt->
                            doOnUiThread {
                                tvSleepValue?.let {tvSleepValueIt->
                                    val mValue2 = "${dataIt.total_score.toInt()}\n"
                                    SpanUtils.create()
                                        .addForeColorSection(mValue2, getColor(R.color.c_f360a9))
                                        .setAbsSize(mValue2, 44)
                                        .setStyle(mValue2, Typeface.BOLD)
                                        .addSection(dataIt.getGradeStr())
                                        .showIn(tvSleepValueIt)
                                }
                            }
                        }
                    }
                }
        }
    }
}
